<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
class UserController extends Controller
{
    public function getIndex(Request $request){
       $id = $request -> session() -> get('id');
        $data = DB::table('shop_user') -> where('id',$id) -> first();
        return view('home/user/index',['data' => $data]);
    }

    public function getAdress(Request $request){
        $uid = $request -> session() -> get('id');
        $data = DB::table('shop_getaddr') -> where('uid',$uid) -> get();
        $data1 = DB::table('shop_getaddr') -> where('uid',$uid) ->where('status',1)-> first();
        //dd($data);
        return view('home/user/adress',['data' => $data, 'data1' =>$data1]);
    }

    public function postAdressadd(Request $request){
        //dd($request -> all());
         /*"_token" => "oY8edE7Unor6vr66aNbMmcbm8Q0CWJgwkmmZx886"
          "name" => "刘佩佩"
          "tel" => "15939223871"
          "province" => "河南省"
          "country" => "鹤壁市"
          "town" => "鹤壁市"
          "adress" => "桃花源"*/
           $this -> validate($request,[
            'name' => 'required',
            'tel' => 'required|digits:11',
            'province' => 'required',
            'country' => 'required',
            'town' => 'required',
            'adress' => 'required'
        ],[
            'name.required' => '姓名不存在',
            'tel.required' => '电话不存在',
            'tel.digits' => '电话号码要11位',
            'province.required' => '选择不存在',
            'country.required' => '选择不存在',
            'town.required' => '选择不存在',
            'adress.required' => '地址不存在'
        ]);
          $uid = $request -> session() -> get('id');
          $arr = $request -> except('_token','x','y');
          $arr['uid'] = $uid;
          //dd($arr);
          $res = DB::table('shop_getaddr') -> insert($arr);
          if($res){
            return redirect('home/user/adress ') -> with('success','添加成功');
         }else{
            return back() -> with('error','添加-失败');
         }

    }
          //获取删除数据的id号
         public function getAdressdelete($id){
               
                //删除用户详情表中的数据
                $res = DB::table('shop_getaddr') -> where('id',$id) -> delete();

                //进行判断 如果删除成功跳转至index页面 如果失败,利用事件回滚返回
                if($res){
                    return redirect('home/user/adress') -> with('success','删除成功');
                }else{
                    return back() -> with('error','删除失败');
                }
            }

            public function getAdressedit($id){
                
                $data = DB::table('shop_getaddr') -> where('id',$id) -> first();
                //dd($data);
                return view('home/user/editadress',['data' => $data]);
            }

            public function postUpdateadress(Request $request){
                //dd($request -> all());
                 $arr = $request -> except(['_token','id','x','y']);
                $res = DB::table('shop_getaddr') -> where('id',$request->input('id')) -> update($arr);


                if($res){
                return redirect('home/user/adress') -> with('success','修改成功');
                }else{
                    return back() -> with('error','修改失败');
                }
            }

            public function getAjax(Request $request){
                
                $id = $request->input('id');
                $data1 = DB::table('shop_getaddr') -> update(['status' => 0]);
                $data2 = DB::table('shop_getaddr') -> where('id',$id) -> update(['status' => 1]);
                   
            }

            public function postEditpic(Request $request){
              //dd($request -> all());
              //验证是否有文件上传
              if($request -> hasFile('userpic')){

                //如果有文件上传 先随机再拼接上传文件的名称
                $temp_name = md5(time().rand(0,999999));
                $hz = $request -> file('userpic') -> getClientOriginalExtension();
                $userpic = $temp_name.'.'.$hz;

                //把接收的文件移动到头像上传的文件夹
                $request -> file('userpic') -> move('./uploads/images/userpic/',$userpic);

                //把需要的数据存入数组 去除不需要的数据
                $arr = $request -> except(['_token','id']);
                $arr['userpic'] = $userpic;
                //dd($arr);
                $res = DB::table('shop_user') -> where('id',$request -> input('id')) -> update($arr);
               
                if($res){
                    $data = DB::table('shop_user') -> where('id',$request -> input('id')) -> get();
                    return back() -> with('success','修改成功');
                }
              }else{
                  return back();
              }
            }  

            public function postUpdate(Request $request){
                //dd($request -> all());
               $this -> validate($request,[
              'username' => 'required',
              'email' => 'email',
              'tel' => 'digits:11'
            ],[
              'username.required' => '用户名不存在',
              'email.email' => '邮箱格式不正确',
              'tel.digits' => '电话号码要11位'
            ]);
              $arr = $request -> except('_token','id');
              //dd($arr);
             $res = DB::table('shop_user') -> where('id',$request -> input('id')) -> update($arr);
             if($res){
                return redirect('/home/user/index');
             }
            }

            public function getCenter(Request $request){
              $id = $request -> session() -> get('id');
              $data = DB::table('shop_user') -> where('id',$id) -> first();
              return view('home/user/center',['data' => $data]);
          }

            public function getInformation(Request $request){
                $id = $request -> session() -> get('id');
                $uid = $id;
                $data = DB::table('shop_userdetails') -> where('uid',$uid) ->first();
                return view('home/user/information',['data' => $data]);
            }

            public function postInforupdate(Request $request){
              //dd($request -> all());
              $arr = $request -> except('_token','id');
              $uid = $request -> input('id');

              //$res = DB::table('shop_userdetails') -> where('uid',$uid) -> insert($arr);
              $res = DB::table('shop_userdetails') -> where('uid',$request -> input('id')) -> update($arr);
              //dd($res);
              if($res){
                return redirect('home/user/information');
             }
            }
}
